import random

import requests, re
from bs4 import BeautifulSoup


def translate(keyword):
    url = f'https://cn.bing.com/dict/search?q={keyword}&qs=n&form=Z9LH5&sp=-1&pq=perform&sc=8-7&sk=&cvid=7CDE031B23F54AA9941B123E3D4BE4AE&ghsh=0&ghacc=0&ghpl='
    response = requests.get(url)
    text = response.text
    soup = BeautifulSoup(text, 'lxml')
    result = soup.find('div', class_='qdef').find('ul').text
    return result


def xh():
    xh_list = []
    url = 'http://v.juhe.cn/joke/randJoke.php?key=e16064c35cf9032231439a89a64c1c55'
    response = requests.get(url)
    text = response.json()['result']
    for item in text:
        xh_list.append(re.sub('\s+', '', item['content']))
    # print(xh_list)
    result = random.choice(xh_list)
    return result


# 周公解梦
def oneiromancy(keyword):
    str1 = ''
    key = 'bdb8ca9135bccb72753c473ff6c659aa'
    url = 'http://v.juhe.cn/dream/query'
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    data = {
        'q': keyword,
        'cid': '',
        'full': '',
        'key': key,
    }
    response = requests.post(url, headers=headers, data=data)
    text = response.json()['result']
    for item in text:
        title = item['title']
        des = item['des']
        str1 += f'【{title}】\n【解梦】：{des}\n\n'
    # print(str1)
    return str1


# 星座运势
def constellation(keyword):
    str1 = ''
    key = 'a0e5d681192ac9dacf7b7efb347450a1'
    url = 'http://web.juhe.cn/constellation/getAll'
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    data = {
        'consName': keyword,
        'type': 'tomorrow',
        'key': key,
    }
    response = requests.post(url, headers=headers, data=data)
    text = response.json()
    name = text['name']
    datetime = text['datetime']
    QFriend = text['QFriend']
    color = text['color']
    health = text['health']
    love = text['love']
    work = text['work']
    money = text['money']
    number = text['number']
    summary = text['summary']
    all = text['all']
    str1 += f'【{name}座明日运势】\n幸运色：{color}\n健康指数:{health}\n爱情指数:{love}\n工作指数:{work}\n财运指数：{money}\n幸运数字:{number}\n速配星座:{QFriend}\n明日概述：{summary}\n综合运势：{all}'
    # print(str1)
    return str1


if __name__ == '__main__':
    # translate('周工解梦')
    # oneiromancy('钱')
    res = xh()
    print(res)
